<html>
<head><meta charset="utf-8"><title>IndexMut for HashMap · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/IndexMut.20for.20HashMap.html">IndexMut for HashMap</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="240285032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/IndexMut%20for%20HashMap/near/240285032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/IndexMut.20for.20HashMap.html#240285032">(May 26 2021 at 06:49)</a>:</h4>
<p>This seems like a conspicuous absence. Why doesn't <code>HashMap</code> implement <code>IndexMut</code>, given that it does have <code>Index</code> and you can write <code>get_mut(idx).unwrap()</code> for a similar effect?</p>



<a name="240285642"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/IndexMut%20for%20HashMap/near/240285642" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/IndexMut.20for.20HashMap.html#240285642">(May 26 2021 at 06:59)</a>:</h4>
<p>I think because in a lot of languages you can use that for insertion, and it would be confusing that that causes panics</p>



<a name="240285727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/IndexMut%20for%20HashMap/near/240285727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/IndexMut.20for.20HashMap.html#240285727">(May 26 2021 at 07:00)</a>:</h4>
<p>(however, using it for insertion in rust is impossible, unless the type implements Default or something — you need to give the assignment a target)</p>



<a name="240291243"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/IndexMut%20for%20HashMap/near/240291243" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/IndexMut.20for.20HashMap.html#240291243">(May 26 2021 at 08:09)</a>:</h4>
<p>I think there's also been the idea of possibly having <code>m[k] = v;</code> through some kind of an <code>IndexSet</code>, but that can't be added later if it has an <code>IndexMut</code> now.</p>



<a name="240336054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/IndexMut%20for%20HashMap/near/240336054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/IndexMut.20for.20HashMap.html#240336054">(May 26 2021 at 14:47)</a>:</h4>
<p><a href="https://stackoverflow.com/q/30414424/155423">How can I update a value in a mutable HashMap?</a></p>



<a name="240336106"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/IndexMut%20for%20HashMap/near/240336106" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/IndexMut.20for.20HashMap.html#240336106">(May 26 2021 at 14:47)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/pull/23559">Future-proof indexing on maps: remove IndexMut</a></p>



<a name="240340035"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/IndexMut%20for%20HashMap/near/240340035" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/IndexMut.20for.20HashMap.html#240340035">(May 26 2021 at 15:10)</a>:</h4>
<p>sadly it wasn't future proof enough, or we could index and indexmut to get Entry values (i think the traits allow that?)</p>



<a name="240347798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/IndexMut%20for%20HashMap/near/240347798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/IndexMut.20for.20HashMap.html#240347798">(May 26 2021 at 15:59)</a>:</h4>
<p>No, you have to return a reference from <code>index</code>/<code>index_mut</code></p>



<a name="240350337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/IndexMut%20for%20HashMap/near/240350337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/IndexMut.20for.20HashMap.html#240350337">(May 26 2021 at 16:14)</a>:</h4>
<p>I'll be sad if an <code>IndexSet</code> trait collides with the <a href="https://docs.rs/indexmap/1.6.2/indexmap/set/struct.IndexSet.html">collection</a></p>



<a name="240350371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/IndexMut%20for%20HashMap/near/240350371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/IndexMut.20for.20HashMap.html#240350371">(May 26 2021 at 16:15)</a>:</h4>
<p>it would be possible to work with that, but just annoying</p>



<a name="240351342"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/IndexMut%20for%20HashMap/near/240351342" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/IndexMut.20for.20HashMap.html#240351342">(May 26 2021 at 16:22)</a>:</h4>
<p>(and FWIW, we do <code>impl IndexMut&lt;_&gt; for IndexMap</code>)</p>



<a name="240369659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/IndexMut%20for%20HashMap/near/240369659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/IndexMut.20for.20HashMap.html#240369659">(May 26 2021 at 18:32)</a>:</h4>
<p>IndexGet and IndexSet aren't very difficult traits to design (I wrote an RFC a while back), but it seems like something that's not a priority compared to everything else.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>